<?php

namespace app\http\common\trait;

use app\enum\ResponseEnum;

trait ResponseTrait
{
    /**
     * 构造失败数组
     * @param string $msg
     * @param int    $code
     * @param array  $data
     * @return array
     */
    public function error(string $msg = '请求失败', int $code = ResponseEnum::ERROR, array &$data = []): array
    {
        return [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ];
    }

    /**
     * 构造成功数组
     * @param array  $data
     * @param string $msg
     * @param int    $code
     * @return array
     */
    public function success(array &$data = [], string $msg = '请求成功', int $code = ResponseEnum::SUCCESS): array
    {
        return [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ];
    }
}